package com.gzhryc.shared_device.oem.code.member.services;

import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.base.models.OrderLogContent;
import com.gzhryc.shared_device.oem.code.member.dao.MemberTransferMoneyOrderLogDao;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberTransferMoneyOrderLog;

import java.sql.SQLException;
import java.util.Date;

public class MemberTransferMoneyOrderLogService {

    static Logger log = Logger.getLogger(MemberTransferMoneyOrderLogService.class);

    public static MemberTransferMoneyOrderLogService self() {
        return MultiDBTools.getService(MemberTransferMoneyOrderLogService.class, true);
    }

    public static MemberTransferMoneyOrderLogService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, MemberTransferMoneyOrderLogService.class, true);
    }

    MemberTransferMoneyOrderLogDao dao;

    public MemberTransferMoneyOrderLogService(String jdbcKey) {
        dao = new MemberTransferMoneyOrderLogDao(jdbcKey);
    }

    public MemberTransferMoneyOrderLog getByTradeNo(String tradeNo){
        try {
            return dao.getById(tradeNo);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean save(String tradeNo,Date createDate,OrderLogContent.LogItem logItem){
        MemberTransferMoneyOrderLog orderLog = getByTradeNo(tradeNo);
        if(orderLog != null){
            OrderLogContent logContent = null;
            if(JsonTools.isJsonStr(orderLog.getContent())) {
                logContent = JsonTools.fromJson(orderLog.getContent(), OrderLogContent.class);
                logContent.getItems().add(logItem);
            }else{
                logContent = new OrderLogContent();
                logContent.getItems().add(logItem);
            }

            MemberTransferMoneyOrderLog updateEntity = new MemberTransferMoneyOrderLog();
            updateEntity.setTradeNo(tradeNo);
            updateEntity.setContent(JsonTools.toJson(logContent));
            try {
                return dao.updateNotNull(updateEntity) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }else {
            OrderLogContent logContent = new OrderLogContent();
            logContent.getItems().add(logItem);

            orderLog = new MemberTransferMoneyOrderLog();
            orderLog.setTradeNo(tradeNo);
            orderLog.setContent(JsonTools.toJson(logContent));
            orderLog.setCreateDate(createDate);
            try {
                return dao.insert(orderLog)> 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean addLogItem(String tradeNo,Date orderCreateDate,OperateInfo operateInfo, String content, Integer state, String errorMsg){
        OrderLogContent.LogItem logItem = new OrderLogContent.LogItem();
        logItem.setCreateDate(new Date());
        logItem.setContent(content);
        logItem.setProgramType(operateInfo.getProgramType());
        logItem.setProgramTypeName(operateInfo.getProgramTypeName());
        logItem.setOperatorType(operateInfo.getOperatorType());
        logItem.setOperatorTypeName(operateInfo.getOperatorTypeName());
        logItem.setOperator(operateInfo.getOperator());

        logItem.setState(state);
        logItem.setErrorMsg(errorMsg);

        return save(tradeNo,orderCreateDate,logItem);
    }
    
    public boolean deleteByTradeNo(String tradeNo) {
    	try {
			if(dao.deleteBranchById(tradeNo) >= 0) {
				return true;
			}
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return false;
    }
}
